/**
 * MasicSequencer
 * <p/>
 * User: joakimd
 * Date: 2013-03-11
 * Time: 15:33
 */
package org.dru.masic {
	import org.dru.masic.utils.Logger;

public class MasicSequencer {
    private var _container: MasicContainer;
    private var _playing: Boolean;
    private var _timing: MasicTiming;
    private var _tick: int;
    private var _currentSamplesToTick: int;

    public function MasicSequencer() {
        _container = new MasicContainer();
        _playing = false;
        _timing = new MasicTiming();
        _currentSamplesToTick = MasicConstants.SAMPLE_BUFFER_SIZE;
    }

    public final function get container(): MasicContainer {
        return _container;
    }

    protected final function get timing(): MasicTiming {
        return _timing;
    }

    public final function get playing(): Boolean {
        return _playing;
    }

    public final function set playing(value: Boolean): void {
        if (value != _playing) {
            _playing = value;
            if (value) {
                _currentSamplesToTick = 0;
                _tick = 0;
                Logger.log(Logger.INFO, "playing");
            }
            else {
                _currentSamplesToTick = MasicConstants.SAMPLE_BUFFER_SIZE;
                Logger.log(Logger.INFO, "not playing");
            }
        }
    }

    public function get tick(): int {
        return _tick;
    }

    public final function get currentSamplesToTick(): int {
        return _currentSamplesToTick;
    }

    internal final function nextSamplesToTick(): void {
        _currentSamplesToTick = _timing.toSample(_tick + 1) - _timing.toSample(_tick);
        _tick++;
    }

    internal final function countDownSamplesToTick(amount: int): void {
        if (amount < 0 || amount > _currentSamplesToTick) {
            throw new Error("amount out of range");
        }
        if (playing) {
            _currentSamplesToTick -= amount;
        }
    }


    public function processTick(): void {
    }
}
}
